import request from "@/utils/request"

export const verifyApi = async (data: Record<string, string>, chunkCount: number) => {
  const res = await request.get(`file/verify`, {
    params: { ...data, chunkCount }
  })

  return res.data
}

export const uploadFileApi = async (formData: FormData) => {
  const res = await request.post('file/uploadFile', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })

  return res.data
}

export const uploadChunkApi = async (formData: FormData) => {
  const res = await request.post('file/uploadChunk', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })

  return res.data
}

export const mergeApi = async (params: Record<string, any>) => {
  const res = await request.get(`file/merge`, { params })

  return res.data
}

export const removeApi = async (filePath: string) => {
  const res = await request({ url: `file/remove`, method: 'delete', params: {filePath} })

  return res.data
}

export const fileSizeApi = async (filePath: string) => {
  const res = await request.get(`file/fileSize`, { params: {filePath} })

  return res.data
}

export const downloadFileApi = async (filePath: string) => {
  return await request.get(`file/downloadFile`, { params: {filePath}, responseType: 'blob' })
}

export const downloadChunkApi = async (filePath: string, offset, length) => {
  return await request.get(`file/downloadChunk`, { params: { filePath, offset, length }, responseType: 'blob' })
}

export const getDirectoriesApi = async () => {
  const res = await request.get(`file/getDirectories`)

  return res.data
}

export const getFilesApi = async (path: string) => {
  const params = {path}
  const res = await request.get(`file/getFiles`, { params })

  return res.data
}